#include "min.h"	// 函数的声明、结构体的声明和定义、宏定义

LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam);

/* @@@@ Windows 程序必需1：WinMain函数 */
/* 这个函数相当于控制台程序的main函数 */
int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR szCmdLine, int iCmdShow)
{
	MSG msg;
	HWND hwnd;
	static char szAppName[] = "myclass";

	/* @@@@ Windows 程序必需2：注册wndclassex */
	WNDCLASSEX wndclassex;
	wndclassex.cbSize = sizeof(WNDCLASSEX);
	wndclassex.cbClsExtra = 0;
	wndclassex.cbWndExtra = 0;
	wndclassex.hInstance = hInstance;
	wndclassex.hIcon = LoadIconA (NULL, IDI_APPLICATION);
	wndclassex.hCursor = LoadCursorA (NULL, IDC_ARROW); 
	wndclassex.hbrBackground = (HBRUSH) GetStockObject (WHITE_BRUSH);
	wndclassex.lpfnWndProc = WndProc;
	wndclassex.lpszMenuName = NULL;
	wndclassex.lpszClassName = szAppName;
	wndclassex.hIconSm = wndclassex.hIcon; 
	wndclassex.style = CS_HREDRAW | CS_VREDRAW;
	if (!RegisterClassExA (&wndclassex)) { 
		MessageBoxA (NULL, "RegisterClassEx failed!", szAppName, MB_ICONERROR); 
		return 0; 
	}

	/* 在这里创建一个窗口并将它显示出来，输出文字的功能是在回调函数中实现的。 */
	hwnd = CreateWindowExA (WS_EX_OVERLAPPEDWINDOW, szAppName, "WindowTitle", WS_OVERLAPPEDWINDOW, 
			CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, NULL, NULL, hInstance, NULL);

	ShowWindow (hwnd, 1); 
	UpdateWindow (hwnd); 

	
	/* @@@@ Windows 程序必需3：消息循环处理 */
	while (GetMessageA (&msg, NULL, 0, 0)) 
	{
		TranslateMessage (&msg); 
		DispatchMessageA (&msg);
	}
	return msg.wParam;
}


/* @@@@ Windows 程序必需4：窗口回调函数 */
LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) 
{
	HDC hdc;
	PAINTSTRUCT ps;
	switch (message) 
	{
		case WM_CREATE:
			return 0;
		case WM_PAINT:
			hdc = BeginPaint (hwnd, &ps); 
			TextOutA (hdc, 0, 0, "Hello world!", 12);
			TextOutA (hdc, 32, 32, "Win32 API!", 9);
			EndPaint (hwnd, &ps);
			return 0; 
		case WM_DESTROY:
			PostQuitMessage(0); 
			return 0;
	}
	return DefWindowProcA (hwnd, message, wParam, lParam);
}
